import 'dart:convert';

import 'package:flutter/services.dart';
import 'package:get/get.dart';

import '../assets/images/mine/model/mine_model.dart';
import '../common/common_config.dart';
import '../common/router.dart';
import '../page/login/bean/region_model.dart';
import 'toast_utils.dart';

class AppUtil{

  //复制内容
  static copyData(String? data, {String toastMsg = ''}) {
    if (data != null && data != '') {
      Clipboard.setData(ClipboardData(text: data));
      if(toastMsg.isNotEmpty){
        showToast(toastMsg);
      }else{
        showToast('copy_success'.tr);
      }
    }

  }

  ///获取国家代码
  static Future<List<RegionModel>> getRegionList() async {
    List<RegionModel> dataSource = [];
    var value = await rootBundle.loadString(configRegionJson);
    if (value.isNotEmpty) {
      var arr = json.decode(value);
      if (arr is List) {
        for (Map item in arr) {
          dataSource.add(RegionModel.fromJson(item));
        }
      }
    }
    return dataSource;
  }

  ///获取我的Json数据
  static Future<List<MineModel>> getMineJsonData() async {
    List<MineModel> dataSource = [];
    var value = await rootBundle.loadString(configMineJson);
    if (value.isNotEmpty) {
      var arr = json.decode(value);
      for (var item in arr) {
        dataSource.add(MineModel.fromJson(item));
      }
    }
    return dataSource;
  }

  ///导航返回根路径
  static void backNavRoot(){
    Get.until((route) {
      bool ok = route.settings.name == Routers.tabBar;
      return ok;
    });
  }

}